import qt_shiyong_rc
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import *
from mysql import mysql
import cv2
import time
from get_face_encoding import get_encoding
from get_face_encoding import to_str
import face_recognition
from PyQt5.QtCore import *
from PyQt5.QtGui import *

class xinxi_caiji(object):
    def setupUi(self, xinxi_caiji):
        xinxi_caiji.setObjectName("xinxi_caiji")
        xinxi_caiji.resize(840,770)
        
        xinxi_caiji.setMinimumSize(QtCore.QSize(840, 770))
        xinxi_caiji.setMaximumSize(QtCore.QSize(840, 770))
        xinxi_caiji.setStyleSheet("background-image: url(:/新前缀/好看的图片.jpg);")
        
        self.label = QtWidgets.QLabel(xinxi_caiji)
        self.label.setGeometry(QtCore.QRect(240, 10, 471, 51))
        font = QtGui.QFont()
        font.setFamily("黑体")
        font.setPointSize(22)
        self.label.setFont(font)
        self.label.setObjectName("label")
        self.cai_btn_shuju = QtWidgets.QPushButton(xinxi_caiji)
        self.cai_btn_shuju.setGeometry(QtCore.QRect(20, 510, 121, 101))
        font = QtGui.QFont()
        font.setFamily("Agency FB")
        font.setPointSize(16)
        self.cai_btn_shuju.setFont(font)
        self.cai_btn_shuju.setStyleSheet("image: url(:/新前缀/数据采集.png);")
        self.cai_btn_shuju.setText("")
        self.cai_btn_shuju.setObjectName("cai_btn_shuju")
        self.cai_btn_jichu = QtWidgets.QPushButton(xinxi_caiji)
        self.cai_btn_jichu.setGeometry(QtCore.QRect(20, 640, 421, 101))
        font = QtGui.QFont()
        font.setFamily("Agency FB")
        font.setPointSize(16)
        self.cai_btn_jichu.setFont(font)
        self.cai_btn_jichu.setStyleSheet("image: url(:/新前缀/查询 (1).png);")
        self.cai_btn_jichu.setText("")
        self.cai_btn_jichu.setObjectName("cai_btn_jichu")
        
        self.cai_jichu = QtWidgets.QTextBrowser(xinxi_caiji)
        self.cai_jichu.setGeometry(QtCore.QRect(460, 530, 371, 211))
        self.cai_jichu.setStyleSheet("border-image: url(:/新前缀/好看的图片.jpg);")
        self.cai_jichu.setObjectName("cai_jichu")
        font = QtGui.QFont()
        font.setFamily("黑体")
        font.setPointSize(16)
        self.cai_jichu.setFont(font)
        self.layoutWidget = QtWidgets.QWidget(xinxi_caiji)
        self.layoutWidget.setGeometry(QtCore.QRect(620, 70, 201, 411))
        self.layoutWidget.setObjectName("layoutWidget")
        self.verticalLayout = QtWidgets.QVBoxLayout(self.layoutWidget)
        self.verticalLayout.setContentsMargins(0, 0, 0, 0)
        self.verticalLayout.setObjectName("verticalLayout")
        self.horizontalLayout_3 = QtWidgets.QHBoxLayout()
        self.horizontalLayout_3.setObjectName("horizontalLayout_3")
        self.number = QtWidgets.QLabel(self.layoutWidget)
        self.number.setMinimumSize(QtCore.QSize(50, 50))
        self.number.setStyleSheet("")
        self.number.setObjectName("number")
        self.horizontalLayout_3.addWidget(self.number)
        self.caiji_xuehao = QtWidgets.QLineEdit(self.layoutWidget)
        self.caiji_xuehao.setObjectName("caiji_xuehao")
        self.horizontalLayout_3.addWidget(self.caiji_xuehao)
        self.verticalLayout.addLayout(self.horizontalLayout_3)
        self.horizontalLayout = QtWidgets.QHBoxLayout()
        self.horizontalLayout.setObjectName("horizontalLayout")
        self.name = QtWidgets.QLabel(self.layoutWidget)
        self.name.setMinimumSize(QtCore.QSize(50, 50))
        self.name.setStyleSheet("")
        self.name.setObjectName("name")
        self.horizontalLayout.addWidget(self.name)
        self.caiji_xingming = QtWidgets.QLineEdit(self.layoutWidget)
        self.caiji_xingming.setObjectName("caiji_xingming")
        self.horizontalLayout.addWidget(self.caiji_xingming)
        self.verticalLayout.addLayout(self.horizontalLayout)
        self.horizontalLayout_2 = QtWidgets.QHBoxLayout()
        self.horizontalLayout_2.setObjectName("horizontalLayout_2")
        self.class_2 = QtWidgets.QLabel(self.layoutWidget)
        self.class_2.setMinimumSize(QtCore.QSize(50, 50))
        self.class_2.setStyleSheet("")
        self.class_2.setObjectName("class_2")
        self.horizontalLayout_2.addWidget(self.class_2)
        self.caiji_banji = QtWidgets.QLineEdit(self.layoutWidget)
        self.caiji_banji.setObjectName("caiji_banji")
        self.horizontalLayout_2.addWidget(self.caiji_banji)
        self.verticalLayout.addLayout(self.horizontalLayout_2)
        self.horizontalLayout_4 = QtWidgets.QHBoxLayout()
        self.horizontalLayout_4.setObjectName("horizontalLayout_4")
        self.class_3 = QtWidgets.QLabel(self.layoutWidget)
        self.class_3.setMinimumSize(QtCore.QSize(50, 50))
        self.class_3.setStyleSheet("")
        self.class_3.setObjectName("class_3")
        self.horizontalLayout_4.addWidget(self.class_3)
        self.caiji_xingbie = QtWidgets.QLineEdit(self.layoutWidget)
        self.caiji_xingbie.setObjectName("caiji_xingbie")
        self.horizontalLayout_4.addWidget(self.caiji_xingbie)
        self.verticalLayout.addLayout(self.horizontalLayout_4)
        self.horizontalLayout_5 = QtWidgets.QHBoxLayout()
        self.horizontalLayout_5.setObjectName("horizontalLayout_5")
        self.name_2 = QtWidgets.QLabel(self.layoutWidget)
        self.name_2.setMinimumSize(QtCore.QSize(50, 50))
        self.name_2.setStyleSheet("")
        self.name_2.setObjectName("name_2")
        self.horizontalLayout_5.addWidget(self.name_2)
        self.caiji_dianhua = QtWidgets.QLineEdit(self.layoutWidget)
        self.caiji_dianhua.setObjectName("caiji_dianhua")
        self.horizontalLayout_5.addWidget(self.caiji_dianhua)
        self.verticalLayout.addLayout(self.horizontalLayout_5)
        self.label_2 = QtWidgets.QLabel(xinxi_caiji)
        self.label_2.setGeometry(QtCore.QRect(460, 490, 121, 41))
        font = QtGui.QFont()
        font.setFamily("Agency FB")
        font.setPointSize(16)
        self.label_2.setFont(font)
        self.label_2.setObjectName("label_2")
        self.cai_btn_renlian = QtWidgets.QPushButton(xinxi_caiji)
        self.cai_btn_renlian.setGeometry(QtCore.QRect(310, 510, 131, 101))
        font = QtGui.QFont()
        font.setFamily("Agency FB")
        font.setPointSize(16)
        self.cai_btn_renlian.setFont(font)
        self.cai_btn_renlian.setStyleSheet("image: url(:/新前缀/人脸信息管理.png);")
        self.cai_btn_renlian.setText("")
        self.cai_btn_renlian.setObjectName("cai_btn_renlian")
        self.cai_btn_xiangji = QtWidgets.QPushButton(xinxi_caiji)
        self.cai_btn_xiangji.setGeometry(QtCore.QRect(160, 510, 131, 101))
        font = QtGui.QFont()
        font.setFamily("Agency FB")
        font.setPointSize(16)
        self.cai_btn_xiangji.setFont(font)
        self.cai_btn_xiangji.setStyleSheet("image: url(:/新前缀/打开相机.png);")
        self.cai_btn_xiangji.setText("")
        self.cai_btn_xiangji.setObjectName("cai_btn_xiangji")
        self.caiji_xiangji = QtWidgets.QLabel(xinxi_caiji)
       
        self.caiji_zhuangtailan = QtWidgets.QTextBrowser(xinxi_caiji)
        self.caiji_zhuangtailan.setGeometry(QtCore.QRect(0, 741, 841, 31))
        self.caiji_zhuangtailan.setObjectName("caiji_zhuangtailan")

        self.retranslateUi(xinxi_caiji)
        QtCore.QMetaObject.connectSlotsByName(xinxi_caiji)
        self.new_set()

    def retranslateUi(self, xinxi_caiji):
        _translate = QtCore.QCoreApplication.translate
        xinxi_caiji.setWindowTitle(_translate("xinxi_caiji", "信息采集系统"))
        self.label.setText(_translate("xinxi_caiji", "人脸识别个人信息采集系统"))
        self.number.setText(_translate("xinxi_caiji", "学号："))
        self.name.setText(_translate("xinxi_caiji", "姓名："))
        self.class_2.setText(_translate("xinxi_caiji", "班级："))
        self.class_3.setText(_translate("xinxi_caiji", "性别："))
        self.name_2.setText(_translate("xinxi_caiji", "电话："))
        self.label_2.setText(_translate("xinxi_caiji", "基本信息"))
        self.caiji_zhuangtailan.setHtml("信息提示")
        
        
    
    def new_set(self):
        self.cai_btn_shuju.clicked.connect(self.shuju_caiji)
        self.cai_btn_xiangji.clicked.connect(self.dakai_xiangji)
        self.cai_btn_renlian.clicked.connect(self.renlian_caiji)
        self.cai_btn_jichu.clicked.connect(self.chaxun_jichu)
        
        # 给按键设置提示
        self.cai_btn_shuju.setToolTip("基础信息采集")
        self.cai_btn_xiangji.setToolTip("打开相机")
        self.cai_btn_renlian.setToolTip("人脸采集")
        self.cai_btn_jichu.setToolTip("基础信息查询(仅输入学号即可)")
        self.caiji_xingbie.setToolTip("1:代表男生，2:代表女生")

        self.xiangji_zhuangtai = 1 # 定义一个变量，用来打开和关闭相机
        
        
    def shuju_caiji(self):
        id = self.caiji_xuehao.text()
        name = self.caiji_xingming.text()
        gender = self.caiji_xingbie.text()
        phone = self.caiji_dianhua.text()
        Class = self.caiji_banji.text()
        if id and name and gender and phone and Class:
            self.caiji_zhuangtailan.setHtml("数据正在采集，请稍等")
            insert = mysql(name,Class,id,gender,phone)
            state = insert.insert_basics()

            if state==1:
                self.caiji_zhuangtailan.setHtml("采集成功")
            else:
                self.caiji_zhuangtailan.setHtml("数据采集失败，出错代码：" + str(state))
        else:
            self.caiji_zhuangtailan.setHtml("请填写全部信息后在进行采集")

    def dakai_xiangji(self):
        if self.xiangji_zhuangtai == 1:
            self.caiji_xiangji.setGeometry(QtCore.QRect(40, 90, 530, 390))
            self.caiji_xiangji.setObjectName("caiji_xiangji")
            self.caiji_zhuangtailan.setHtml("相机正在打开，请稍等")
            # 打开相机
            self.capture = cv2.VideoCapture(0)  
            self.caiji_zhuangtailan.setHtml("相机已经打开")
            self.xiangji_zhuangtai=2
            ret, self.frame = self.capture.read()
            self.frame=cv2.cvtColor(self.frame,cv2.COLOR_BGR2RGB)
            self.Qframe=QImage(self.frame.data,self.frame.shape[1],self.frame.shape[0],self.frame.shape[1]*3,QImage.Format_RGB888)
            self.caiji_xiangji.setPixmap(QPixmap.fromImage(self.Qframe))
              
        else:
            self.xiangji_zhuangtai = 1 
            self.capture.release()
            self.caiji_xiangji.setGeometry(QtCore.QRect(1000, 90, 1001, 390))
            self.caiji_zhuangtailan.setHtml("相机已经关闭")
                
    def renlian_caiji(self):
        
        id = self.caiji_xuehao.text()
        if id and self.xiangji_zhuangtai ==2:
            self.caiji_zhuangtailan.setHtml("人脸信息正在采集，请注视摄像头，请稍等")
            # 读取图像信息
            face_encoding = get_encoding(self.frame)
            face_str = to_str(face_encoding)

            insert_face = mysql(id= id ,face = face_str)
            state = insert_face.insert_face()
            if state ==1:
                self.caiji_zhuangtailan.setHtml("人脸信息插入成功")
            else:
                self.caiji_zhuangtailan.setHtml("人脸信息插入失败，错误代码：" +  str(state))
        else:
            self.caiji_zhuangtailan.setHtml("请先填写学号并且打开相机在进行人脸采集")


    def chaxun_jichu(self):
        
        id = self.caiji_xuehao.text()
        if id:
            self.caiji_zhuangtailan.setHtml("基础信息正在查询，请稍等")
            lookup = mysql(id = id)
            state = lookup.lookup()
            if state==1:
                xinxi='学号：'+str(lookup.id)+'；\n 姓名：'+str(lookup.name)+'；\n 班级：'+str(lookup.banji)+"；\n性别："+str(lookup.gender)+"；\n电话："+str(lookup.phone) 
                self.cai_jichu.setHtml(xinxi)
                self.caiji_zhuangtailan.setHtml("已显示基础信息")
            else:
                self.caiji_zhuangtailan.setHtml("查询数据出错，出错代码：" +  str(state))
        else:
            self.caiji_zhuangtailan.setHtml("请输入学号后进行信息查询")
    
